package org.yoyo.classroom.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.yoyo.classroom.domain.po.StudyRoom;
import org.yoyo.classroom.domain.po.StudyRoomCategory;
import org.yoyo.classroom.domain.po.StudyRoomFacility;
import org.yoyo.classroom.domain.vo.StudyRoomDetailVO;
import org.yoyo.classroom.domain.vo.StudyRoomReviewSummary;
import org.yoyo.classroom.mapper.BookingOrderMapper;
import org.yoyo.classroom.result.Result;
import org.yoyo.classroom.service.BookingOrderService;
import org.yoyo.classroom.service.StudyRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 自习室控制器
 */
@RestController
@RequestMapping("/study/studyroom/")
public class StudyRoomController {

    @Autowired
    private StudyRoomService studyRoomService;

    @Autowired
    private BookingOrderService bookingOrderService;

    /**
     * 根据ID查询自习室
     * @param id 自习室ID
     * @return 包含自习室对象的结果
     */
    @GetMapping("/{id}")
    public Result<StudyRoom> getStudyRoomById(@PathVariable Long id) {
        StudyRoom studyRoom = studyRoomService.getStudyRoomById(id);
        if (studyRoom != null) {
            return Result.success(studyRoom);
        } else {
            return Result.error("Study room not found");
        }
    }

    /**
     * 查询所有自习室
     * @return 包含自习室列表的结果
     */
    @GetMapping("/findAll")
    public Result<List<StudyRoom>> getAllStudyRooms() {
        List<StudyRoom> studyRooms = studyRoomService.getAllStudyRooms();
        return Result.success(studyRooms);
    }

    /**
     * 插入自习室
     * @param studyRoom 自习室对象
     * @return 包含影响行数的结果
     */
    @PostMapping("/")
    public Result<Integer> addStudyRoom(@RequestBody StudyRoom studyRoom) {
        int affectedRows = studyRoomService.addStudyRoom(studyRoom);
        return Result.success(affectedRows);
    }

    /**
     * 更新自习室
     * @param studyRoom 自习室对象
     * @return 包含影响行数的结果
     */
    @PutMapping("/")
    public Result<Integer> updateStudyRoom(@RequestBody StudyRoom studyRoom) {
        int affectedRows = studyRoomService.updateStudyRoom(studyRoom);
        return Result.success(affectedRows);
    }

    /**
     * 删除自习室（逻辑删除）
     * @param id 自习室ID
     * @return 包含影响行数的结果
     */
    @DeleteMapping("/{id}")
    public Result<Integer> deleteStudyRoom(@PathVariable Long id) {
        int affectedRows = studyRoomService.deleteStudyRoom(id);
        return Result.success(affectedRows);
    }


    /**
     * 获取评分最高的三个自习室
     * @return 包含自习室评分摘要列表的结果
     */
    @GetMapping("/top-rated")
    public Result<List<StudyRoomReviewSummary>> getTopRatedStudyRooms() {
        List<StudyRoomReviewSummary> topRatedStudyRooms = studyRoomService.getTopRatedStudyRooms();
        if (topRatedStudyRooms != null && !topRatedStudyRooms.isEmpty()) {
            return Result.success(topRatedStudyRooms);
        } else {
            return Result.error("No study rooms found");
        }
    }


    /**
     * 获取自习室详情页
     * @return 详细数据
     */
    @GetMapping("/details/{roomId}")
    public Result<StudyRoomDetailVO> getStudyRoomDetail(@PathVariable("roomId") Integer roomId) {
        StudyRoomDetailVO studyRoomDetail = studyRoomService.getStudyRoomDetail(roomId);
        if (studyRoomDetail != null) {
            return Result.success(studyRoomDetail);
        } else {
            return Result.error("No study rooms found");
        }
    }


    /**
     * 根据过滤条件查询自习室
     * @param page 页码
     * @param size 每页大小
     * @param type 类型
     * @param facilities 设施
     * @param sort 排序方式
     * @return 包含分页自习室列表的结果
     */
    @GetMapping("filter")
    public Result<Page<StudyRoom>> getStudyRoomsByFilters(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) String facilities,
            @RequestParam(required = false) String sort) {
        Page<StudyRoom> studyRooms = studyRoomService.getStudyRoomsByFilters(page, size, type, facilities, sort);
        if (studyRooms != null) {
            return Result.success(studyRooms);
        } else {
            return Result.error("No study rooms found");
        }
    }


    @GetMapping("/categories")
    public List<StudyRoomCategory> getAllCategories() {
        return studyRoomService.getAllCategories();
    }

    @GetMapping("/facilities")
    public List<StudyRoomFacility> getAllStudyRoomFacilities() {
        return studyRoomService.getAllStudyRoomFacilities();
    }

    @GetMapping("/seatList/{id}")
    public Result getSeatList(@PathVariable("id") Long roomId){
        Result result = new Result();
        result.setData(studyRoomService.getSeatsWithBookingStatus(roomId));
        return result;
    }

    @GetMapping("/seatInfo")
    public Result getSeatInfo(@RequestParam("roomId") Long roomId,
                              @RequestParam("seatId") Long seatId){
        Result result = new Result();
        result.setData(bookingOrderService.getAvailableBookingTimes(roomId,seatId));
        return result;
    }

    @GetMapping("/booking-rule/{id}")
    public Result getRoomRuleById(@PathVariable("id") Long roomId){
        Result result = new Result();
        result.setData(studyRoomService.getRoomRuleById(roomId));
        return result;
    }


}
